﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;//添加
using System.Net;//添加
using System.Net.Mail;
using System.Collections;//添加

namespace Golf.WebUI.App_code
{
    /// <summary>
    ///SentEmail 的摘要说明
    /// </summary>
    public class SentEmail
    {
        #region  第一中方法
        /// <summary>
        /// 发送邮箱  方法
        /// </summary>
        /// <param name="Email">发送的邮箱</param>
        /// <param name="Password">发送的邮箱密码</param>
        /// <param name="ToEmail">要发的邮件地址 群发请用,隔开</param>
        /// <param name="flag">是否群发</param>
        /// <returns></returns>
        public static bool EmailPost(string Email, string Password, string ToEmail, bool flag)
        {
            if (flag)
            {
                string[] EmailStr = ToEmail.Split(',');//取到多个邮件地址
                bool Error = false;
                foreach (var item in EmailStr)
                {
                    Error = SentEmail.ToPost(Email, Password, item);
                }
                return Error;
            }
            else
            {
                return SentEmail.ToPost(Email, Password, ToEmail);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Password"></param>
        /// <param name="ToEmail"></param>
        /// <returns></returns>
        public static bool ToPost(string Email, string Password, string ToEmail)
        {

            /*
             SmtpClient smtp = new SmtpClient("smtp.163.com", 25);
这么一句，说明发送邮件的邮箱是163邮箱，所以配置其他邮箱应该改成对应的参数端口
             */
            Encoding Encode = Encoding.UTF8;
            System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
            Message.From = new MailAddress(Email, "Golf  购物网站管理中心", Encode);
            Message.To.Add(new MailAddress(ToEmail, "尊敬的用户", Encode));
            Message.Subject = "邮件主题" + DateTime.Now.ToLongTimeString();
            Message.SubjectEncoding = Encode;
            //CC为抄送，即发给用户的同时，在发送Email
            Message.CC.Add(new MailAddress(Email, "Golf  购物网站管理中心", Encode));
            StringBuilder MailContent = new StringBuilder();
            MailContent.Append("尊敬的用户：<br/>");
            MailContent.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好！你于");
            MailContent.Append(DateTime.Now.ToLongTimeString());
            MailContent.Append("通过<a href='#'>某某网站</a>注册会员。");
            MailContent.Append("<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;为了安全起见，请用户点击以下链接重设个人密码：");
            string url = "http://www.baidu.com?email=" + ToEmail;
            MailContent.Append("<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='" + url + "'>" + url + "</a>");
            Message.Body = MailContent.ToString();
            Message.BodyEncoding = Encode;
            Message.IsBodyHtml = true;

            try
            {
                SmtpClient smtp = new SmtpClient("smtp.163.com", 25);
                smtp.Credentials = new NetworkCredential(Email, Password);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Send(Message);
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                Message.Dispose();
            }
            return true;
        }

        #endregion

        #region 第二种方法

        /// <summary>
        /// 邮件发送方法(带附件)
        /// </summary>
        /// <param name="mailto">收件人地址。如：receiver@163.com</param>
        /// <param name="mailsubject">邮件标题</param>
        /// <param name="mailbody">邮件正文</param>
        /// <param name="mailFrom">邮件发送人地址。如：sender@163.com</param>
        /// <param name="list">附件路径</param>
        /// <returns></returns>
        public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
        {
            try
            {
                //邮件发送人地址
                System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom);
                //如test@163.com,初步测试，用test@sina.com不行，用163的邮件服务器，就必须用163邮箱的用户名
                //收件人地址
                System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如test@tom.com
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
                mail.Subject = mailsubject;
                mail.Body = mailbody;
                //以下设置服务器
                System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient();
                //以下为增加附件
                int count = list.Count;
                for (int i = 0; i < count; i++)
                {
                    System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list[i].ToString());
                    mail.Attachments.Add(data);
                }
                mySmth.Send(mail);
                mail.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

    }
}
